﻿using System.Collections.Generic;
using System.Web.Mvc;

namespace ValidationFramework.Web.MVC
{
	public static class ModelStateBinder
	{
		public static void UpdateModelState(IList<ValidationResult> validationResults, ModelStateDictionary modelState)
		{
			// skip if there are no validation errors
			if (validationResults.Count <= 0)
			{
			    return;
			}

			foreach (var result in validationResults)
			{
				var key = result.InfoDescriptor.Name;
				modelState.AddModelError(key, result.AttemptedValue.ToString(), result.ErrorMessage);
			}
		}
	}
}
